package com.laboratory.serviceprovide.workBench.service.impl;

import com.Laboratory.util.timeUtil;
import com.Laboratory.utilPojo.projectState;
import com.Laboratory.utilPojo.projectWithState;
import com.Laboratory.utilPojo.projectWithUrl;
import com.Laboratory.workBench.pojo.file;
import com.Laboratory.workBench.pojo.project;
import com.Laboratory.workBench.pojo.teacher;
import com.Laboratory.workBench.service.projectService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laboratory.serviceprovide.constant.constantArgs;
import com.laboratory.serviceprovide.workBench.mapper.projectMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/25 9:21
 * @Version 1.0
 */

@DubboService(interfaceClass = projectService.class,version = "1.0.0")
public class projectServiceImpl implements projectService {
    @Resource
    private projectMapper projectMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<teacher> getTeacherListByApply() {
        List<teacher>list=null;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if (ops.get(constantArgs.REDIS_TEACHER_LIST_KEY)==null){
            list=projectMapper.getTeacherListByApply();
            if(list.size()>0) {
                ops.set(constantArgs.REDIS_TEACHER_LIST_KEY, JSON.toJSONString(list), 30 * 60, TimeUnit.SECONDS);
            }
        }else{
            list= JSON.parseObject(ops.get(constantArgs.REDIS_TEACHER_LIST_KEY), new TypeReference<List<teacher>>() {
            });
        }
        return list;
    }

    @Override
    public int insertProjectApply(project project) {
        int i = projectMapper.insertProjectApply(project);
        if(i>0){
            projectState projectState=new projectState();
            projectState.setId(UUID.randomUUID().toString().replace("-","").toUpperCase());
            projectState.setProjectId(project.getId());
            projectState.setProjectState("0");
            projectState.setCreateTime(timeUtil.getNowTime(new Date()));
            projectMapper.insertProjectDescription(projectState);
        }
        return i;
    }

    @Override
    public void updateRedisCacheByInsertApply(List<projectWithUrl> list){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String s = ops.get(constantArgs.REDIS_PROJECT_FAIL_LIST_KEY);
        if(s!=null){
            List<projectWithUrl> list1 = JSON.parseObject(s, new TypeReference<List<projectWithUrl>>() {
            });
            for (projectWithUrl url:
                 list) {
                list1.add(0,url);
            }
            ops.setIfPresent(constantArgs.REDIS_PROJECT_FAIL_LIST_KEY,
                    JSON.toJSONString(list1),
                    60*60*6,
                    TimeUnit.SECONDS);
        }
    }

    @Override
    public int saveFileToLocalService(List<file> FileList) {

        return projectMapper.saveFileToLocalService(FileList);
    }

    @Override
    public List<Map<Object, Object>> getTeacherPopularity() {
        return projectMapper.getTeacherPopularity();
    }

    @Override
    public List<Map<Object, Object>> getCostRankData() {
        return projectMapper.getCostRankData();
    }

    @Override
    public List<projectWithState> getProjectList() {
        return projectMapper.getProjectList();
    }
}
